package org.cainiao.basic.invoke;

import java.lang.invoke.*;
import java.util.Arrays;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 */
public class CallSiteTest {

    private static final MethodHandle printArgs;

    static {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        Class thisClass = lookup.lookupClass(); // (who am I?)
        try {
            printArgs = lookup.findStatic(thisClass, "printArgs", MethodType.methodType(void.class, Object[].class));
        } catch (NoSuchMethodException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
        // ignore caller and name, but match the type:
        return new ConstantCallSite(printArgs.asType(type));
    }

    static void test() throws Throwable {
        // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
        // InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
    }

    private static void printArgs(Object... args) {
        System.out.println(Arrays.deepToString(args));
    }

    public static void main(String[] args) {

    }
}
